package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.StateForm;
import com.woniuxy.controller.info.DefectStateIdAndState;
import com.woniuxy.controller.info.DefectstateNamesInfo;
import com.woniuxy.controller.info.StateInfo;
import com.woniuxy.controller.info.UserInfo;
import com.woniuxy.model.Defectstate;
import com.woniuxy.service.DefectstateService;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.MyException;
import com.woniuxy.utils.Result;
import com.woniuxy.utils.TimeMethodsutil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@RestController
@Api(tags = "缺陷状态接口")
@RequestMapping("/defectstate")
public class DefectstateController {
    @Resource
    private DefectstateService defectstateService;



    /**
     * 描述：查询所有
     * @return all
     */
    @PostMapping("/findAll")
    @ApiOperation("查询缺陷状态")
    public Result findAll(){
        List<StateInfo> all = defectstateService.findAll();

        return Result.success(200,"success",all);
    }
    @PostMapping("/selectByState")
    public Result selectByState(@RequestBody StateForm stateForm){

        String Sign = stateForm.getDefectstateSign();
       List<StateInfo> stateInfos = defectstateService.selectByState(Sign);
            return Result.success(200,"success",stateInfos);
    }

    /**
     * 描述：新增缺陷状态
     * @return num
     */
    @ApiOperation("新增缺陷状态")
    @PostMapping("/addState")
    public Result addPriority(@RequestBody StateForm stateForm) throws MyException {

        //获取用户信息
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String nowTimeToString = TimeMethodsutil.getNowTimeToString();
        stateForm.setDefectstateCreatename(userInfo.getUsername());
        stateForm.setDefectstateCreatenameId(userInfo.getId());
        stateForm.setDefectstateCreatetime(nowTimeToString);
        Integer add= defectstateService.add(stateForm);
        if(add>0){
            return Result.success(200,"success",add);
        }else{
            throw new MyException(405,"新增失败");
        }

    }


    /**
     * 描述：批量删除
     * @return delectid
     */
    @ApiOperation("批量删除")
    @PostMapping("/delectMany")
    public Result delectMany(@RequestBody List<Integer> ids) {
        boolean b = defectstateService.removeByIds(ids);
        return Result.success(200, "success", b);
    }

    /**
     * 描述：单个删除
     * @return delectid
     */
    @ApiOperation("单个删除")
    @PostMapping("/delectbyid/{defectstateId}")
    public  Result delectByid(@PathVariable Integer defectstateId) throws MyException {

        boolean b = defectstateService.removeById(defectstateId);
        if (b){
            return Result.success(200,"success",b);
        }else {
            throw new MyException(405,"删除失败");
        }

    }

    /**
     * 修改严重程度
     * @param stateForm
     * @return
     * @throws MyException
     */
    @ApiOperation("修改严重程度")
    @PostMapping("/update")
    public Result updatePriority(@RequestBody StateForm stateForm) throws MyException {

        boolean change = defectstateService.change(stateForm);

        if (change){
            return Result.success(200,"success",change);
        }else {
            throw new MyException(405,"服务器繁忙");
        }
    }


    /**
     * 修改状态
     * @param stateForm
     * @return
     */
    @ApiOperation("修改状态")
    @PostMapping("/changeState")
    public Result changeState(@RequestBody StateForm stateForm){
        Integer integer = defectstateService.changeState(stateForm);
        return Result.success(200,"success",integer);
    }

    /**
     * 获取缺陷有效状态
     * @return defectStateIdAndStates
     */
    @ApiOperation("获取缺陷有效状态")
    @PostMapping("/getdefectStateIsOk")
    public Result getdefectStateIsOk(){
        List<DefectStateIdAndState> defectStateIdAndStates= defectstateService.getdefectStateIsOk();

        return Result.success(200,"查询成功",defectStateIdAndStates);
    }

    /**
     * 获取所有状态
     * @return
     * @throws MyException
     */
    @ApiOperation("获取所有状态")
    @PostMapping("/getAllStates")
    public Result getAllStates() throws MyException {

        List<Defectstate> list = defectstateService.list();
        List<DefectstateNamesInfo> defectstateNamesInfos = CopyUtil.copyList(list, DefectstateNamesInfo.class);

        if(null == defectstateNamesInfos) throw new MyException(500,"查询失败");
        return Result.success(200,"查询成功",defectstateNamesInfos);
    }

    /**
     * 获取已分配后的缺陷状态
     * @return
     */
    @ApiOperation("获取已分配后的缺陷状态")
    @PostMapping("/getDefectNoInitState")
    public Result getDefectNoInitState(){

        List<DefectStateIdAndState> defectStateIdAndStates=defectstateService.getDefectNoInitState();
        return Result.success(200,"查询成功",defectStateIdAndStates);
    }

    /**
     * 获取分配中的可操作状态
     * @return
     */
    @ApiOperation("获取分配中的可操作状态")
    @PostMapping("/getChoiceStates")
    public Result getChoiceStates(){
        List<DefectstateNamesInfo> defectstateNamesInfos=defectstateService.getChoiceStates();
        return Result.success(200,"查询成功",defectstateNamesInfos);
    }


}

